package yt.kpool.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.king.base.adapter.ViewHolderAdapter;
import com.king.base.adapter.holder.ViewHolder;

import java.util.List;

import yt.kpool.R;
import yt.kpool.bean.MinerGroupBean;
import yt.kpool.util.Util;
import yt.kpool.view.dialog.AddGroupDialog;
import yt.kpool.view.dialog.DelectGroupDialog;


/**
 * 矿工分组选择适配器
 * Created by Administrator on 2017/7/17.
 */

public class MinerGroupSelectListviewAdapter extends ViewHolderAdapter<MinerGroupBean.DataBean> {

    private String CurrencyID;
    private String ChildAccountID;
    private OnClickItemListener onClickItemListener;
    private int selectedPos;//选中的id 没有为-1
    private boolean isFastlogin;

    private int groupId;

    public MinerGroupSelectListviewAdapter(int groupId,boolean isFastlogin,Context context, String CurrencyID, String ChildAccountID, List<MinerGroupBean.DataBean> listData, OnClickItemListener onClickItemListener) {
        super(context, listData);
        this.groupId=groupId;
        this.CurrencyID=CurrencyID;
        this.isFastlogin=isFastlogin;
        this.ChildAccountID=ChildAccountID;
        this.onClickItemListener=onClickItemListener;
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, MinerGroupBean.DataBean dataBean, int position) {
        return layoutInflater.inflate(R.layout.item_minergroup_listview,null);
    }

    @Override
    public void bindViewDatas(ViewHolder holder, final MinerGroupBean.DataBean dataBean, int position) {
        holder.setText(R.id.tv_content,dataBean.getName());
        holder.setText(R.id.tv_Hashrate, Util.FormatHashrate(Double.valueOf(dataBean.getHashrate())));
        holder.setText(R.id.all,dataBean.getOnNumber()+dataBean.getOffNumber()+dataBean.getLoseNumber()+"");
        holder.setText(R.id.OnNumber,dataBean.getOnNumber()+"");
        holder.setText(R.id.OffNumber,dataBean.getOffNumber()+"");
        holder.setText(R.id.LoseNumber,dataBean.getLoseNumber()+"");
        if ("全部".equals(dataBean.getName())||"未分组".equals(dataBean.getName())|| isFastlogin)
        {
            holder.getView(R.id.iv_edit).setVisibility(View.GONE);
            holder.getView(R.id.iv_del).setVisibility(View.GONE);
        }else {
            holder.getView(R.id.iv_edit).setVisibility(View.VISIBLE);
            holder.getView(R.id.iv_del).setVisibility(View.VISIBLE);
        }
        if (dataBean.getID()==groupId)
        {
            //选中的项
            ((TextView)holder.getView(R.id.tv_content)).setTextColor(context.getResources().getColor(R.color.system_color));
        }else
        {
            ((TextView)holder.getView(R.id.tv_content)).setTextColor(context.getResources().getColor(R.color.black1));
        }

        holder.getView(R.id.iv_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickItemListener.clickItem();
                new AddGroupDialog(context, dataBean.getName(),dataBean.getID(),"",CurrencyID,ChildAccountID, new AddGroupDialog.OnSuccessListener() {
                    @Override
                    public void getResult() {

                    }
                }).show();
            }
        });
        holder.getView(R.id.iv_del).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickItemListener.clickItem();
                new DelectGroupDialog(context, dataBean.getName(),dataBean.getID(),new DelectGroupDialog.OnSuccessListener() {
                    @Override
                    public void getResult() {

                    }
                }).show();
            }
        });
    }
    /**
     * 设置选中
     * @param i
     */
    public void setSelected(int i)
    {
        selectedPos=i;
        notifyDataSetChanged();
    }

    /**
     * 设置选中id
     */
    public int getSelectedPos() {
        return selectedPos;
    }

    /**
     * 点击回调
     */
    public interface OnClickItemListener {
        public void clickItem();

    }
}
